package com.hetao.xx.service;

import android.annotation.SuppressLint;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.hetao.xx.Global;
import com.hetao.xx.constants;
import com.hetao.xx.orm.UserVo;

import java.util.HashMap;
import java.util.Map;

public class UserService {

    class UserLevel {
        public int upExperience;
        public int userLevel;
        public int nextLevel;
        public float enhance;
        public String name;
    }

    private final int maxLevel = 10;
    private Map<Integer, UserLevel> map;
    public UserService() {
        init();
    }

    /**
     * 初始等级信息
     */
    @SuppressLint("DefaultLocale")
    private void init() {
        map = new HashMap<>(4);
        String [] levelList = new String[] {"练气","筑基","金丹","元婴","化神","合道","地仙","天仙","金仙",
                "太乙金仙","大罗金仙","准圣","圣人","天道","大道"};

        int baseExperience = 100;
        int experience = 0;
        for (int bigLevel=0; bigLevel<levelList.length; bigLevel++) {
            int bigExperience = baseExperience * (1+bigLevel*3);

            for (int i=0; i<maxLevel; i++) {
                experience += bigExperience;

                int level = bigLevel*10+i;
                UserLevel bean = new UserLevel();
                bean.userLevel = level+1;
                if (levelList.length == bigLevel && i+1 == maxLevel) {
                    bean.nextLevel = -1;
                } else {
                    bean.nextLevel = bean.userLevel + 1;
                }
                bean.upExperience = experience;
                bean.enhance = (100f + level*20) / 100f;
                bean.name = String.format("%s%d级", levelList[bigLevel], i+1);
                map.put(bean.userLevel, bean);
            }
        }
        Log.i("userLevel", "map: " + JSONObject.toJSONString(map));

    }


    /***
     *  用户升级
     * @param userVo
     * @return
     */
    public boolean upUserLevel(UserVo userVo) {
        if (userVo.userLevel == map.size()) {
            Global.getInstant().toastMsg("等级已满，无法升级");
            return false;
        }

        UserLevel level = map.get(userVo.userLevel+1);
        if (level == null) {
            Global.getInstant().toastMsg("未找到升级目标，吱吱吱~");
            return false;
        }

        if (level.upExperience > userVo.experience) {
            Global.getInstant().toastMsg("经验不足，需要经验："+level.upExperience);
            return false;
        }

        userVo.experience -= level.upExperience;
        userVo.userLevel = level.userLevel;
        upUserLevel(userVo, level);
        UserManager.saveAll();

        Global.getInstant().toastMsg("升级成功, 现在等级"+userVo.userLevel);
        Global.getInstant().updateUserInfo();
        return true;
    }

    /**
     * 修为名称
     */
    public String getName(int level) {
        UserLevel userLevel = map.get(level);
        if (userLevel == null) {
            return "未知";
        }

        return userLevel.name;
    }

    /**
     * 用户升级
     * @param
     * @return
     */
    public void upUserLevel(UserVo userVo, UserLevel level) {
        userVo.Hp = (int) (constants.oriHp * level.enhance);
        userVo.Mp = (int) (constants.oriMp * level.enhance);
        userVo.attack  = (int) (constants.oriAttack * level.enhance);
        userVo.defence  = (int) (constants.oriDefence * level.enhance);

    }
}
